package com.wd.visitor.common.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * @className: TimeUtils
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2024/11/3
 **/
public class TimeUtils {

    public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String parseStr(Long timestamp) {
        // 转换为 LocalDateTime
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS);
        String formattedTime = dateTime.format(formatter);
        return formattedTime;
    }

    public static long parseTimestamp(String dateTimeStr) {

        // 将字符串解析为 LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr.replace(" ", "T"));
        long timestamp = localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toEpochSecond();
        return timestamp;
    }

    public static String getStartTime()
    {
        LocalDateTime today = LocalDateTime.now().withHour(0).withMinute(0).withSecond(1).withNano(0);
        // 指定使用北京时区
        ZoneId beijingZone = ZoneId.of("Asia/Shanghai");

        // 如果需要以字符串形式输出时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
       return today.atZone(beijingZone).format(formatter);
    }

    public static long getStartTimeStamp()
    {
        // 设置北京时区
        ZoneId beijingZone = ZoneId.of("Asia/Shanghai");

        // 获取今天的日期
        LocalDate today = LocalDate.now(beijingZone);

        // 创建当天 0 点 0 分 1 秒的时间
        ZonedDateTime zdt = today.atStartOfDay(beijingZone).plusSeconds(1);

        // 获取时间戳（秒）
        long timestampInSeconds = zdt.toEpochSecond();
        return timestampInSeconds;
    }

    public static String getEndTime(){
        // 获取当前日期并设置为当天的 23:59:59
        LocalDateTime todayEnd = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(0);

        // 指定使用北京时区
        ZoneId beijingZone = ZoneId.of("Asia/Shanghai");

        // 如果需要以字符串形式输出时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        return todayEnd.atZone(beijingZone).format(formatter);
    }

    public static long getCurrentTime(){
        long timestampInMillis = System.currentTimeMillis();
        // 转换为秒
        long timestampInSeconds = timestampInMillis / 1000;
        return timestampInSeconds;
    }

}
